package org.example.springaop.blog_demo;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/10/23
 * Time: 00:46
 */
@Component
@Slf4j
@Aspect
@Order(1)
public class AOP1 {

    @Pointcut("execution(* org.example.springaop.controller.Controller.*(..))")
    public void pointcut(){}

    @Around("pointcut()")
    //@Around("execution(* org.example.springaop.controller.Controller.*(..))")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("doAround1:业务执行前");
        //执行业务
        Object result = joinPoint.proceed();
        //计算时间
        log.info("doAround1:业务执行后,result:{}", result);
        return "success1";
    }

    @Before("pointcut()")
    public void doBefore(){
        log.info("doBefore1");
    }

    @After("pointcut()")
    public void doAfter(){
        log.info("doAfter1");
    }

    @AfterReturning(value = "pointcut()",returning = "id")
    public void doAfterReturning(Integer id){
        //发生异常时不执行
        log.info("doAfterReturning1,id:{}", id);
    }

    @AfterThrowing(value = "pointcut()",throwing = "throwable")
    public void doAfterThrowing(Throwable throwable){
        log.info("doAfterThrowing1,throwable:{}", throwable.getMessage());
    }

    @Around("@annotation(org.example.springaop.config.CustomizeAspect)")
    public Object customize(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("customize before1");
        Object result = joinPoint.proceed();
        log.info("customize after1,result:{}", result);
        return "success1";
    }
}
